package com.yeqing.crazymakercircle.bufferDemo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class UseSocket {


    public static void useSocket() {
        try (
                // 创建SocketChannel
                SocketChannel socketChannel = SocketChannel.open();
                ) {
            // 发起连接
            socketChannel.connect(new InetSocketAddress(7777));
            // 设置为非阻塞
            socketChannel.configureBlocking(false);

            while(!socketChannel.finishConnect()){
                System.out.println("正在连接……");
                // 不断地自旋、等待，或者做一些其他的事情……
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        useSocket();
    }
}
